package com.pks.autoInsurance.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pks.autoInsurance.dto.VehicleDetailsDto;
import com.pks.autoInsurance.helper.AdminHelper;

/**
 * Servlet implementation class AdminDeleteUser Perform activities for managing
 * users
 */
public class AdminUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doPost::::begin");
		String action = request.getParameter("action");
		String userId = request.getParameter("userId");
		System.out.println("action: " + action + " userId: " + userId);
		AdminHelper adminHelper = new AdminHelper();
		if ("delete-user".equals(action)) {
			System.out.println("::::action=delete-user");
			int deletedUsers = adminHelper.deleteUser(userId);
			request.setAttribute("message", deletedUsers + " User is deleted ");
		} else if ("edit-user".equals(action)) {
			System.out.println("::::action=edit-user");
			VehicleDetailsDto vehicleDetailsDto = adminHelper
					.getUserVehicleDetail(userId);
			System.out.println("::::vechileDetailsDto::::" + vehicleDetailsDto);
			request.setAttribute("vehicleDetailsDto", vehicleDetailsDto);
			request.getRequestDispatcher("adminEdit.jsp").forward(request,
					response);
			return;
		} else if ("search-user".equals(action)) {
			System.out.println("::::search-user");
			// Retrive list of all the users matching given userId
			if ("".equals(userId)) {
				request.setAttribute("personalDetailsDtos",
						new AdminHelper().getUsersList());
			}
			request.setAttribute("personalDetailsDtos",
					new AdminHelper().getUsersList(userId));
			request.getRequestDispatcher("adminManage.jsp").forward(request,
					response);
			return;
		} else if ("update-user".equals(action)) {
			System.out.println("::::update-user");
			String vehicleRegistrationNo = request
					.getParameter("vehicleRegistrationNo");
			String policyAmount = request.getParameter("policyAmount");
			int amount = ("".equals(policyAmount)) ? 0 : Integer
					.parseInt(policyAmount);
			VehicleDetailsDto vehicleDetailsDto = new VehicleDetailsDto();
			vehicleDetailsDto.setUserId(userId);
			vehicleDetailsDto.setVehicleRegistrationNo(vehicleRegistrationNo);
			vehicleDetailsDto.setPolicyAmount(amount);
			int updatedUsers = adminHelper.updateUser(vehicleDetailsDto);
			System.out.println("::::usersUpdated=" + updatedUsers);
			request.setAttribute("message", updatedUsers + " User is updated ");
		}

		// Retrive list of all the users
		request.setAttribute("personalDetailsDtos",
				new AdminHelper().getUsersList());
		request.getRequestDispatcher("adminManage.jsp").forward(request,
				response);
		System.out.println(getClass() + "::::doPost::::end");
	}

}
